using System.Runtime.CompilerServices;
using DG.Tweening;
using UnityEngine;

public class LevelFinishActivator : MonoBehaviour
{
	public delegate void LevelFinishActivatorEventHandler(Transform levelTransform);

	public LayerMask interactionLayer;

	public GameObject buttonBody;

	public Material materialActivated;

	public Material materialDeactivated;

	public bool isActivated;

	private Vector3 activatedScale = new Vector3(1.4f, 1.4f, 0.75f);

	private bool isActive;

	private Vector3 initialMovingPartScale;

	[method: MethodImpl(32)]
	public static event LevelFinishActivatorEventHandler LevelFinishActivatorOnEvent;

	public static void LevelFinishActivatorOn(Transform levelTransform)
	{
		if (LevelFinishActivator.LevelFinishActivatorOnEvent != null)
		{
			LevelFinishActivator.LevelFinishActivatorOnEvent(levelTransform);
		}
	}

	private void Awake()
	{
		LevelController.LevelReadyEvent += OnLevelReady;
		LevelController.LevelFinishedBeforeEvent += OnLevelFinishedBefore;
		EventManager.WorldResetEvent += OnWorldReset;
	}

	private void OnDestroy()
	{
		LevelController.LevelReadyEvent -= OnLevelReady;
		LevelController.LevelFinishedBeforeEvent -= OnLevelFinishedBefore;
		EventManager.WorldResetEvent -= OnWorldReset;
	}

	private void OnLevelReady(Transform levelTransform)
	{
		if (base.transform.root == levelTransform)
		{
			Activate();
			RecordInitialState();
		}
	}

	private void OnLevelFinishedBefore(Transform levelTransform)
	{
		if (base.transform.root == levelTransform)
		{
			SetActivated();
			ActivateInstant();
		}
	}

	private void OnWorldReset()
	{
		SetDeactivated();
		DeactivateInstant();
	}

	private void RecordInitialState()
	{
		initialMovingPartScale = buttonBody.transform.localScale;
	}

	private void Activate()
	{
		isActive = true;
	}

	private void OnTriggerEnter(Collider other)
	{
		if (isActive && !isActivated && Utils.IsInLayerMask(other.gameObject, interactionLayer))
		{
			SetActivated();
			ActivateAnimated();
			PlayActivationSound();
		}
	}

	private void SetActivated()
	{
		isActivated = true;
		base.GetComponent<Collider>().enabled = false;
	}

	private void SetDeactivated()
	{
		isActivated = false;
		base.GetComponent<Collider>().enabled = true;
	}

	private void ActivateAnimated()
	{
		buttonBody.transform.DOScale(activatedScale, 0.2f).SetEase(Ease.Linear).OnComplete(delegate
		{
			buttonBody.GetComponent<Renderer>().material = materialActivated;
			LevelFinishActivatorOn(base.transform.root);
		});
	}

	public void ActivateInstant()
	{
		SetActivated();
		buttonBody.transform.localScale = activatedScale;
		buttonBody.GetComponent<Renderer>().material = materialActivated;
	}

	private void DeactivateInstant()
	{
		if (initialMovingPartScale != Vector3.zero)
		{
			buttonBody.transform.localScale = initialMovingPartScale;
		}
		buttonBody.GetComponent<Renderer>().material = materialDeactivated;
	}

	private void PlayActivationSound()
	{
		AudioSource audioSource = base.gameObject.GetComponent<AudioSource>();
		if (audioSource == null)
		{
			audioSource = base.gameObject.AddComponent<AudioSource>();
		}
		Audio.PlaySoundGameButton(audioSource);
	}
}
